--[[
    快捷聊天弹窗，支持表情和短语
]]--

local blaze = require "blaze"
local app = require "app"
local game = app.game.running

local M = game.ui.element()

function M:ctor()
    self.res = "club.threecard.gamelib/ui/chat.b:ShortcutPop"
    self.emoji_list = require("club.threecard.gamelib.UI.Chat.EmojiList").new()
    self.phrase_list = require("club.threecard.gamelib.UI.Chat.PhraseList").new()
    self.tab_index = nil
end

function M:on_injected(bind)
    bind("gameobject.element", self.phrase_list_obj, self.phrase_list)
    bind("gameobject.element", self.emoji_list_obj, self.emoji_list)
    bind("toggle.value", self.phrase_tab_toggle, self.on_phrase_tab_toggle)
    bind("toggle.value", self.emoji_tab_toggle, self.on_emoji_tab_toggle)
    bind("button.click", self.mask_btn, self.on_click_mask)
    bind("app.event", "发送聊天表情", self.hide)
    bind("app.event", "发送聊天短语", self.hide)
    if not self.tab_index then
        self:select_tab(0)
    end
end

function M:on_phrase_tab_toggle(on)
    if on then
        self:select_tab(0)
    end
end

function M:on_emoji_tab_toggle(on)
    if on then
        self:select_tab(1)
    end
end

function M:select_tab(index)
    if self.tab_index == index then
        return
    end
    self.tab_index = index
    self:lazy("on_select_tab")
end

function M:on_select_tab()
    local index = self.tab_index
    if index == 0 then
        self.phrase_list:show()
        self.emoji_list:hide()
        self.phrase_tab_toggle.isOn = true
    elseif index == 1 then
        self.phrase_list:hide()
        self.emoji_list:show()
        self.emoji_tab_toggle.isOn = true
    end
end

function M:on_click_mask()
    self:hide()
end

return M